package com.sgb.kjwl.view.ui.activity.self;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.View;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.databinding.ActivitySelfQrBinding;
import com.sgb.kjwl.model.entity.ImAccountBean;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.user.EssentiaIinformationeEntity;
import com.sgb.kjwl.utils.UserInfoCacheUtils;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SelfQrCodeActivity extends AppActivity {

    private ActivitySelfQrBinding binding;

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, SelfQrCodeActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_self_qr);
        binding.include.ivBack.setOnClickListener(this);
        binding.include.titleView.setText("我的二维码");
        UserMsgEntity userMsgEntity = UserInfoCacheUtils.getUserMsgEntity();
        if("".equals(userMsgEntity)||null==userMsgEntity){
            getUserInfo();
        } else{
            binding.userName.setText(userMsgEntity.getRealName());
            binding.companyName.setText(userMsgEntity.getUsername());
        }
        getQrInfo();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    private void getUserInfo() {
        APIManager.getInstance().getUserAPI().getUserInfo().enqueue(new Callback<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserMsgEntity>> call, Response<BaseEntity<UserMsgEntity>> response) {
                if (response.body() == null || response.body().getData() == null) return;
                binding.userName.setText(response.body().getData().getRealName());
                binding.companyName.setText(response.body().getData().getUsername());
            }

            @Override
            public void onFailure(Call<BaseEntity<UserMsgEntity>> call, Throwable t) {
            }
        });
    }

    public void getQrInfo() {
        DialogHelper.showProgressDialog(this, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getMyQrCode().enqueue(new Callback<BaseEntity<ImAccountBean>>() {
            @Override
            public void onResponse(Call<BaseEntity<ImAccountBean>> call, Response<BaseEntity<ImAccountBean>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) return;
                binding.ivQr.setImageBitmap(createQRCode(response.body().getData().getImAccount()));
            }

            @Override
            public void onFailure(Call<BaseEntity<ImAccountBean>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();

            }
        });
    }

    private Bitmap createQRCode(String s) {
        int width = 600;
        int height = 600;
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Map<EncodeHintType, String> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //记得要自定义长宽
        BitMatrix encode = null;
        try {
            encode = qrCodeWriter.encode(s, BarcodeFormat.QR_CODE, width, height, hints);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        int[] colors = new int[width * height];
        //利用for循环将要表示的信息写出来
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                if (encode.get(i, j)) {
                    colors[i * width + j] = Color.BLACK;
                } else {
                    colors[i * width + j] = Color.WHITE;
                }
            }
        }
        return Bitmap.createBitmap(colors, width, height, Bitmap.Config.RGB_565);
    }
}
